﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(login));
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string UserLogin(string account,string pwd)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                DataTable table = ClientInfoBll.ClientLogin(CommonBll.InputText(account, 50), EncryptHelper.EncryptString(pwd), CommonConst.DB_Name);
                if (table.Rows.Count > 0)
                {
                    //判断当天是否已经登录过
                    if (table.Rows[0]["LastLoginTime"] == DBNull.Value || (Convert.ToDateTime(table.Rows[0]["LastLoginTime"].ToString())-DateTime.Now).Days!=0)
                    {
                        //登录时积分+5并更新等级
                        ClientInfoBll.UpdateClientPoints(table.Rows[0]["Client_Id"].ToString(), CommonConst.gLoginPoints, "+", CommonConst.DB_Name);
                    }
                    //更新最后登录时间
                    ClientInfoBll.UpateLastLoginTime(table.Rows[0]["Client_Id"].ToString(), CommonConst.DB_Name);

                    jsonResult.Add("isreload", true);
                    var info = new UserInfo
                                   {
                                       Email =table.Rows[0]["User_Email"] == DBNull.Value? "": table.Rows[0]["User_Email"].ToString(),
                                       Id = table.Rows[0]["Client_Id"].ToString(),
                                       RelName =table.Rows[0]["Client_Name"] == DBNull.Value? "": table.Rows[0]["Client_Name"].ToString(),
                                       Status = table.Rows[0]["User_Status"].ToString(),
                                       UName = table.Rows[0]["Client_Account"].ToString()
                                   };
                    info.Computername = HttpContext.Current.Application["computername"].ToString();
                    info.Computerip = HttpContext.Current.Application["computerip"].ToString();
                    WebMasterCookie.AddCookie(info);

                    Dictionary<string, object> dicLoginLog = new Dictionary<string, object>();
                    dicLoginLog.Add("user_id", info.Id);
                    dicLoginLog.Add("computername", info.Computername);
                    dicLoginLog.Add("computerip", info.Computerip);
                    LogLoginBll.AddLoginLog(dicLoginLog, CommonConst.DB_Name);
                    jsonResult.Add("msg", "/member/message/index.html");
                }
                else
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", "用户名或密码错误！");
                }
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", ex.Message);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";

        }
    }
}
